package com.arpa.ntocc.basic.api.controller;

import cn.hutool.core.bean.BeanUtil;
import com.arpa.core.web.MediaTypes;
import com.arpa.core.web.Result;
import com.arpa.ntocc.common.common.Page;
import com.arpa.ntocc.common.common.constant.StrConsts;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.ntocc.common.controller.BaseController;
import com.arpa.ntocc.common.domain.dto.PartyGroupConfigDTO;
import com.arpa.ntocc.common.domain.entity.PartyGroupConfig;
import com.arpa.ntocc.common.domain.vo.PartyGroupConfigVO;
import com.arpa.ntocc.common.security.annotation.NeedLogin;
import com.arpa.ntocc.common.security.annotation.NoRepeat;
import com.arpa.ntocc.common.service.IPartyGroupConfigService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 机构配置表 Controller
 * </p>
 *
 * @author LEO
 * @since 2020-01-18
 */
@Api(tags = "机构配置表")
@RestController
@RequestMapping(value = "/partyGroupConfig", produces = "application/json;charset=UTF-8")
@Log4j2
public class PartyGroupConfigController extends BaseController {

    @Autowired
    private IPartyGroupConfigService iPartyGroupConfigService;

    /**
     * 获取 机构配置表 列表
     *
     * @param partyGroupConfigDTO
     * @param request
     * @return
     */
    @ApiOperation(value = "获取 机构配置表 列表", notes = "获取 机构配置表 列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = PartyGroupConfigVO.class))
    @NeedLogin
    @GetMapping()
    public Result list(PartyGroupConfigDTO partyGroupConfigDTO, HttpServletRequest request) {
        try {
            PartyGroupConfigVO partyGroupConfigVOSum = iPartyGroupConfigService.queryListSum(partyGroupConfigDTO);
            List<PartyGroupConfigVO> partyGroupConfigVOList = iPartyGroupConfigService.queryList(partyGroupConfigDTO);

            Page returnPage = new Page(partyGroupConfigDTO.getPageNum(), partyGroupConfigDTO.getPageSize());
            returnPage.setRecords(partyGroupConfigVOList);
            returnPage.setTotal(partyGroupConfigVOSum.getTotal());
            returnPage.setSumObject(partyGroupConfigVOSum);
            return Result.ok().data(returnPage);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE, e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 添加 机构配置表
     *
     * @param entity  PartyGroupConfig
     * @param request
     * @return
     */
    @ApiOperation(value = "添加 机构配置表 ", notes = "添加 机构配置表 ")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @NoRepeat(time = 2000)
    @PostMapping()
    public Result create(PartyGroupConfig entity, HttpServletRequest request) {
        try {
            entity.setBranchCode(UserUtil.getBranchCode());
            iPartyGroupConfigService.save(entity);
            return Result.ok("添加成功");
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE, e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取机构配置表详情
     *
     * @param code
     * @param request
     * @return
     */
    @ApiOperation(value = "获取机构配置表详情", notes = "获取机构配置表详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
            @ApiImplicitParam(name = "code", value = "机构配置表code", paramType = "path", dataType = "String")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = PartyGroupConfig.class))
    @NeedLogin
    @GetMapping(value = "/{code}")
    public Result partyGroupConfig(@PathVariable String code, HttpServletRequest request) {
        try {
            PartyGroupConfig entity = iPartyGroupConfigService.getOne(new QueryWrapper<PartyGroupConfig>().lambda().eq(PartyGroupConfig::getCode, code));
            return Result.ok().data(entity);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE, e);
            return Result.error(e.getMessage());
        }
    }


    /**
     * 修改机构配置表
     *
     * @param entityDTO 机构配置表
     * @param request
     * @return
     */
    @ApiOperation(value = "修改机构配置表", notes = "修改机构配置表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PutMapping()
    public Result update(PartyGroupConfigDTO entityDTO, HttpServletRequest request) {
        if (StringUtils.isBlank(entityDTO.getCode())) {
            return Result.error("code不能为空");
        }
        PartyGroupConfig entity = BeanUtil.toBean(entityDTO, PartyGroupConfig.class);
        int updateNum = iPartyGroupConfigService.updateByCode(entity);
        if (updateNum == 0) {
            return Result.error("没有更新数据！code:" + entity.getCode());
        } else {
            return Result.ok("修改成功,更新" + updateNum + "条数据！");
        }
    }

    /**
     * 删除 机构配置表
     *
     * @param codes
     * @param request
     * @return
     */
    @ApiOperation(value = "删除 机构配置表 ", notes = "删除 机构配置表 ", produces = MediaTypes.JSON_UTF_8)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
            @ApiImplicitParam(name = "codes", value = "codes", required = true, paramType = "query", dataType = "List")
    })
    @NeedLogin
    @DeleteMapping(value = "")
    public Result batchdel(@RequestParam List<String> codes, HttpServletRequest request) {
        try {
            iPartyGroupConfigService.removeObj(codes);
            return Result.ok();
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE, e);
            return Result.error(e.getMessage());
        }
    }


    @ApiOperation(value = "获取配置项", notes = "通过配置类型和key获取配置信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
            @ApiImplicitParam(name = "code", value = "机构配置表code", paramType = "path", dataType = "String")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = PartyGroupConfig.class))
    @NeedLogin
    @GetMapping(value = "/{type}/{configKey}")
    public Result getConfig(@PathVariable String type, @PathVariable String configKey, HttpServletRequest request) {
        PartyGroupConfig entity = iPartyGroupConfigService.getOne(new QueryWrapper<PartyGroupConfig>().lambda()
                .eq(PartyGroupConfig::getType, type)
                .eq(PartyGroupConfig::getConfigKey, configKey)
        );
        return Result.ok().data(entity);
    }

}
